//InnerAudioContext实例
//const app = getApp()

 
Component({
/**
   * 组件的属性列表
   */
  properties: {
    tips: {
      type: Array,
      value: [],
      observer: function (n) {
        if (n.length) {
          n.forEach(item => {
            if (item.type == 'warn') {
              item.w = item.tip_content.length * 40;
            } else {
              item.w = item.btn.length * 120;
            }

          })
          this.setData({
            tipsArr: n
          })
        }
      }
    },
    callbackFn: {
      type: Function,
      value: () => {}
    },
    tip0: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip0: n
          })
        }
      }
    },
    tip1: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip1: n
          })
        }
      }
    },
    tip2: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip2: n
          })
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    qiu1:0,
    qiu2:0,
    qiu3:0,
    qiu4:0,
    qiu5:0,
    qiu6:0,
    qiu7:0,
    ok1:'268,',
    answer:0,
    qiu:'',
    num:0,
    btn1:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/upload/202211041325354629.png',
    btn2:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/upload/202211041325354629.png',
    btn3:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/upload/202211041325354629.png',
    btn4:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/upload/202211041325354629.png',
    btn5:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/upload/202211041325354629.png',
    btn6:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/d24b23809e200db5c90d886ea92270d3.png',
    btn7:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/d24b23809e200db5c90d886ea92270d3.png',
    btn8:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/d24b23809e200db5c90d886ea92270d3.png',
    btn9:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/d24b23809e200db5c90d886ea92270d3.png',
    btn:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/4c7920edba7bfafbd342b39d5889a00c.png',
    btnno:'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/images/d24b23809e200db5c90d886ea92270d3.png',
    isShowConfirm:false,
    //answer:'',//输入答案
    tip:'',//提示
    con: '',//答案
    showErr:false,
    showPic:1,
    showPic2:1,
    showPic3:1,
    scancode:'',
    flash:'off',
    iscode:0,
    isios:'',
    play_beijing_music_audio:'',
    code:'',
    txt1:'',
    txt2:'',
    num:0,
  },
  /**
  * 页面加载完成执行
  */
 lifetimes:{
  ready(){
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          //buttonTop1:buttonTop1,//这里定义按钮的初始位置
          //buttonLeft:res.windowWidth*0.70,//这里定义按钮的初始位置
        })
        console.log('sdkver===='+res.SDKVersion)
        that.setData({
          isios:res.platform,
        });
        console.log('isios==='+that.data.isios);
      }
    })
    

  },
  attached: function() {
    // 在组件实例进入页面节点树时执行
  },
  detached: function() {
    // 在组件实例被从页面节点树移除时执行
  },
},
 //组件所在页面的生命周期
 pageLifetimes: {
  show: function() {
    // 页面被展示
  },
  hide: function() {
    // 页面被隐藏
  },
  resize: function(size) {
    // 页面尺寸变化
  }
},
   /**
   * 组件的方法列表
   */
  methods: {
    nowCode: function() {
      var that = this;
      var code = '';
      var ios = that.data.isios;
      console.log('isios==='+ios);
      wx.scanCode({
        onlyFromCamera: false,//仅仅相机
        success: (res) => {
          console.log(res)
          code = res.result;
          that.setData({
            scancode:res.result
            })
  
        },
        //错误返回
        fail: (res) => {
          wx.showToast({
            title: 'fail',
            icon: 'none',
            duration: 1000
          })
        },
        complete: function (res) { 
          that.setData({
            iscode:1
          });
          //that.takeMp3('https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/qingshu_biu.mp3');
          //that.data.play_beijing_music_audio = 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/qingshu_biu.mp3';
          //that.onMusicTap();
          that.nowEnter3();
          console.log('scode is complete');
          // wx.showToast({
          //   title: 'complete',
          //   icon: 'none',
          //   duration: 1000
          // })
         },
      })
    },
    //提示弹窗
    nowEnter3:function(){
      var that = this;
      var tip = '请打开手机音量,观察闪光灯';
      console.log('tip==='+tip)
      that.setData({
        tip:tip,
        isShowConfirm: true,
        iscode:1,
      })
    },
    cancel: function () {
      var that = this
      that.setData({
        isShowConfirm: false,
      })
    },
    confirmAcceptance:function(){
      var that = this
      that.setData({
        isShowConfirm: false,
      })
    },
    nowCodePlay: function() {
      var that = this;
      var code = 'ecuaf';
      var ios = that.data.isios;
      //var code = that.data.scancode;
      console.log('isios==='+ios);
  
            if(code == '9G3iH'){
              console.log('进入==='+code+' isios=='+ios);
              that.takeMp3('https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/qingshu_biu.mp3');
              //that.onMusicTap();
              //if(ios != 'ios'){
                that.takePhoto1();
                setTimeout(function () {
                  //要延时执行的代码
                  that.setData({
                    flash:'off',
                    isShowConfirm: false,
                    })
                 }, 3000) //延迟时间 这里是1秒
              //}
  
            }
            else if(code == 'zivHi'){
              console.log('进入zivHi===');
              that.setData({
                iscode:2
              });
              that.takeMp3('https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/qingshu_323.mp3');
              that.takePhoto1();
              var myDate = new Date();
              var time = myDate.toLocaleString(); 
              var times = 0
       var i = setInterval(function() {
            times++;
            if (times == 3) {
              that.setData({
                flash:'off'
                });
                console.log('执行timer1 end==='+time)
            }
            else if(times == 5){
              that.setData({
                flash:'on'
                });
              console.log('执行timer2 end==='+time)
            }
            else if(times == 8){
              that.setData({
                flash:'off'
                });
              console.log('执行timer3 end==='+time)
            }
            else if(times == 9){
              that.setData({
                flash:'off',
                isShowConfirm: false,
                });
              clearInterval(i)
            }
            else {
                 that.setData({
                      getCode: "重新获取" + times + "s",
                      color: "#999",
                      disabled: true
                 })
            }
       }, 1000);
  
            }
            else if(code == 'ecuaf'||code=='xWBzr'){
              console.log('进入ecuaf或xWBzr==='+code);
              that.takePhoto1();
              that.takeMp3('https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/qingshu_32223.mp3');
              var myDate = new Date();
              var time = myDate.toLocaleString(); 
              var times = 0
       var i = setInterval(function() {
            times++;
            if (times == 3) {
              that.setData({
                flash:'off'
                });
                console.log('执行timer1 end==='+time)
            }
            else if(times == 5){
              that.setData({
                flash:'on'
                });
                console.log('执行timer2 end==='+time)
            }
            else if(times == 7){
              that.setData({
                flash:'off'
                });
                console.log('执行timer3 end==='+time)
            }
            else if(times == 8){
              that.setData({
                flash:'on'
                });
                console.log('执行timer4 end==='+time)
            }
            else if(times == 11){
              that.setData({
                flash:'off'
                });
            }
            else if(times == 12){
              that.setData({
                flash:'off',
                isShowConfirm: false,
                });
              clearInterval(i)
            }
            else {
                 that.setData({
                      getCode: "重新获取" + times + "s",
                      color: "#999",
                      disabled: true
                 })
            }
       }, 1000);
  
            }
  
            that.setData({
              iscode:0
            });
  
       
    },
  
    takePhoto1:function(){
      //let flash = this.data.flash == 'on' ? 'off' : 'on'
      let flash = this.data.flash == 'on' ? 'on' : 'on'
      this.setData({
      flash
      })
      console.log('takePhoto1 end');
    },
    takeMp3:function(mp3){
      console.log('takeMp3==='+mp3);
      //创建一个音频实例
  
      const backgroundAudioManager2 = wx.getBackgroundAudioManager();
        backgroundAudioManager2.title = '情书';
        backgroundAudioManager2.epname = '情书';
        backgroundAudioManager2.singer = '纸谜';
        backgroundAudioManager2.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000';
        backgroundAudioManager2.src = mp3;//this.data.play_beijing_music_audio,
        backgroundAudioManager2.play();
        backgroundAudioManager2.onPlay(() => {
          console.log("音乐播放开始222");
          console.log('音乐播放开始222=====')
        })
        backgroundAudioManager2.onStop(() => {
          console.log("onStop音乐播放结束111");
          console.log('onStop音乐播放停止111===')
        })
        backgroundAudioManager2.onEnded(() => {
          console.log("音乐播放结束222");
          console.log('音乐播放停止222===')
        })
    },
  //逆时针播放按钮事件
  playQiu : function(e){
    var that = this;
    let qiu = e.currentTarget.dataset.qiu;
    let btn = 'btn'+qiu;
    let ok1 = that.data.ok1;
    let btnyes = this.data.btn;
    let btnno = this.data.btnno;
    var answer = that.data.answer;
    let ok = that.data.qiu+qiu;
    let i = that.data.num+1;
    console.log('num='+i)
    if(i%3 ==0){
      ok = ok+',';
      console.log('i='+i+' ok='+ok)
      var index1 = ok1.indexOf(ok);

      console.log('index1=='+index1)
      if(ok1.indexOf(ok)>=0){
        
        wx.showToast({
          title: '答案正确',
          icon: 'success',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
          duration: 2000,      //停留时间
        })
        
      }
      else{
        wx.showToast({
          title: '答案错误',
          icon: 'error',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
          duration: 2000,      //停留时间
        })
        ok = '';
        i=0;
        answer=0;
        btnyes = btnno;
        that.setData({
          'qiu':ok,
          'answer':answer,
          'num':i,
          btn1: btnno,
          btn2: btnno,
          btn3: btnno,
          btn4: btnno,
          btn5: btnno,
          btn6: btnno,
          btn7: btnno,
          btn8: btnno,
          btn9: btnno,
        });
      }
    }
    console.log('qiu='+qiu+' answer='+answer+' btn='+btn)

    that.setData({
      'qiu':ok,
      'answer':answer,
      'num':i,
      [btn]: btnyes,
    });
    
  },
 /*
     * 内部私有方法建议以下划线开头
     * triggerEvent 用于触发事件
     */
    _okEvent(){
      //触发取消回调
      var that = this;
      console.log('触发取消回调')
      var isOk = 0;
      if(that.data.answer==3){
        isOk = 1;
      }
      return isOk;
      //this.triggerEvent("cancelEvent")
    },
    _confirmEvent(){
      //触发成功回调
      this.triggerEvent("confirmEvent");
    },
  //顺时暂停按钮事件
  playForward : function(){
    let gnum = this.data.go_num;
    this.doorChange(gnum);

    // that.setData({
    //   [qiu]:qiu,
    // });
  },
 
  //停止按钮事件
  playRight : function(e){
    var that = this;

    let rnum = this.data.right_num;
    let direction = e.currentTarget.dataset.direction;
    var pan_rotate = that.data.pan_rotate+45;
    that.setData({
      pan_rotate:pan_rotate,
    });
    setTimeout(function () {
      //要延时执行的代码
      that.doorChange(direction);
     }, 300) //延迟时间 这里是1秒

  },
 
  
 

  
  }
})